unit uSAF0021_03;

interface

uses
   Windows,
   Messages,
   SysUtils,
   Variants,
   Classes,
   Graphics,
   Controls,
   Forms,
   Dialogs,
   ComCtrls,
   ToolWin,
   Menus,
   ActnList,
   ImgList,
   ExtCtrls,
   DB,
   ADODB,
   DBCtrls,
   StdCtrls,
   WideStrings,
   FMTBcd,
   Mask;

type
   TfrmInsUpd = class(TForm)
      tlbInsUpd: TToolBar;
      btnSalvar: TToolButton;
      btnS1: TToolButton;
      btnFechar: TToolButton;
      statPrincipal: TStatusBar;
      pnlCentro: TPanel;
      actInsUpd: TActionList;
      actInsUpdSalvar: TAction;
      actInsUpdFechar: TAction;
      ilInsUpd: TImageList;
      dbnvgrLista: TDBNavigator;
      dsTabela: TDataSource;
      edtNUMERO: TDBEdit;
      lblNUMERO: TLabel;
      lblDDI: TLabel;
      edtDDI: TDBEdit;
      lblDDD: TLabel;
      edtDDD: TDBEdit;
      lblID_TIPO_TELEF: TLabel;
      cbbID_TIPO_TELEF: TDBLookupComboBox;
    lblDDIInfo: TLabel;
    lblDDDInfo: TLabel;
    lblNUMEROInfo: TLabel;

      procedure FormShow(Sender: TObject);

      procedure FormKeyPress(Sender: TObject; var Key: Char);
      procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

      procedure actInsUpdSalvarExecute(Sender: TObject);
      procedure actInsUpdFecharExecute(Sender: TObject);
      procedure dsTabelaStateChange(Sender: TObject);
      procedure edtNUMEROExit(Sender: TObject);
    procedure lblDDIInfoClick(Sender: TObject);
    procedure lblDDDInfoClick(Sender: TObject);
    procedure edtNUMEROKeyPress(Sender: TObject; var Key: Char);

   private

      { Private declarations }

   public

      _INSUPD_TIPO: Integer;
      _INSUPD_ID: Integer;
      _INSUPD_ID2: Integer;

      { Public declarations }

   end;

var
   frmInsUpd: TfrmInsUpd;

implementation

{$R *.dfm}

uses
   uLibGlobal,
   uSAF0021_01;

procedure TfrmInsUpd.actInsUpdFecharExecute(Sender: TObject);
begin
   if (DMDados.tblTabela.State in [dsInsert, dsEdit]) then
   begin
      DMDados.tblTabela.Cancel;
   end;

   Self.Close;
end;

procedure TfrmInsUpd.actInsUpdSalvarExecute(Sender: TObject);
var
   chaveID: integer;
begin
   if (DMDados.tblTabela.State in [dsInsert, dsEdit]) then
   begin
      if Application.MessageBox(
         PChar('DESEJA REALMENTE SALVAR O REGISTRO ?'),
         PChar(Application.Title),
         MB_YESNO + MB_ICONQUESTION) = IDYES then
      begin
         DMDados.tblTabela.FieldByName(DMDados._TIPO_CAMPO).AsInteger := DMDados._TIPO_CHAVE;
         DMDados.tblTabela.Post;

         fAtualizaDS(DMDados.qryLista);

         actInsUpdFecharExecute(Sender);
      end;
   end;
end;

procedure TfrmInsUpd.dsTabelaStateChange(Sender: TObject);
begin
   dbnvgrLista.Enabled := not (dsTabela.State in [dsInsert, dsEdit]);

   cbbID_TIPO_TELEF.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   edtDDI.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   edtDDD.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
   edtNUMERO.Enabled := (dsTabela.State in [dsInsert, dsEdit]);

   actInsUpdSalvar.Enabled := (dsTabela.State in [dsInsert, dsEdit]);
end;

procedure TfrmInsUpd.edtNUMEROExit(Sender: TObject);
begin
   actInsUpdSalvarExecute(Sender);
end;

procedure TfrmInsUpd.edtNUMEROKeyPress(Sender: TObject; var Key: Char);
begin
   fTabToEnter(Key, Self);
   fSomenteNumerosTelefone(Key);
end;

procedure TfrmInsUpd.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
   fNotCtrlDelete(Key, Shift);
end;

procedure TfrmInsUpd.FormKeyPress(Sender: TObject; var Key: Char);
begin
   fTabToEnter(Key, Self);
end;

procedure TfrmInsUpd.FormShow(Sender: TObject);
begin
   fMontarTelaInsUpd(Self, 200, 350);

   //CADASTRO DE TIPO DE TELEFONE
   fAtualizaDS(DMDados.tblSAF0017);

   fAtualizaDS(DMDados.tblTabela);

   case _INSUPD_TIPO of
      0: //INSERIR
         begin
            Self.Caption := _sistemaNome + ' - INSERIR REGISTRO';
            DMDados.tblTabela.Insert;
         end;

      1: //EDITAR
         begin
            Self.Caption := _sistemaNome + ' - ALTERAR REGISTRO';
            DMDados.tblTabela.Locate(
               'ID_TELEFONE',
               _INSUPD_ID2, [loCaseInsensitive, loPartialKey]);
            DMDados.tblTabela.Edit;
         end;

      2: //CONSULTAR
         begin
            Self.Caption := _sistemaNome + ' - CONSULTAR REGISTRO';
            DMDados.tblTabela.Locate(
               'ID_TELEFONE',
               _INSUPD_ID2, [loCaseInsensitive, loPartialKey]);
         end;
   end;
end;

procedure TfrmInsUpd.lblDDDInfoClick(Sender: TObject);
begin
   DMDados.tblTabela.FieldByName('DDD').AsString := '11';
end;

procedure TfrmInsUpd.lblDDIInfoClick(Sender: TObject);
begin
   DMDados.tblTabela.FieldByName('DDI').AsString := '+55';
end;

end.

